package com.woniu.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Account;
import com.woniu.mapper.AccountMapper;
import com.woniu.service.AccountService;
import com.woniu.vo.AccountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountMapper accountMapper;

    @Override
    public Account accountLoginByAccountName(String accountName) {
        Example example = new Example(Account.class);
        example.createCriteria().andEqualTo("accountName",accountName);
        return accountMapper.selectOneByExample(example);
    }

    @Override
    public PageInfo<AccountVo> selectAccountByMerchant
            (Integer pageNum, Integer pageSize,String accountName,Long startTime,Long endTime,Integer isDelete) {
        PageHelper.startPage(pageNum, pageSize);
        if (startTime != null && endTime != null) {
            Date startTimedate = new Date(startTime);
            Date endTimedate = new Date(endTime);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTimedate1 = simpleDateFormat.format(startTimedate).substring(0,10)+" 00:00:00";
            
            String endTimedate1 = simpleDateFormat.format(endTimedate).substring(0,10)+" 23:59:59";
            List<AccountVo> list = accountMapper.selectAccountByMerchant(accountName, startTimedate1, endTimedate1, isDelete);
            return new PageInfo<AccountVo>(list);
        }
        List<AccountVo> list = accountMapper.selectAccountByMerchant(accountName, null, null, isDelete);
        return new PageInfo<AccountVo>(list);
    }

    @Override
    public int updateAccountStatusByAccountId(Account account) {
        return accountMapper.updateByPrimaryKeySelective(account);
    }

    @Override
    public Account selectAccountByAccountName(Account account) {
        return accountMapper.selectOne(account);
    }

    @Override
    public int updateAccountByAccountId(Account account) {
        return accountMapper.updateByPrimaryKeySelective(account);
    }

    @Override
    public PageInfo<AccountVo> selectAccountByTotal(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AccountVo> list = accountMapper.selectAccountByTotal();
        return new PageInfo<AccountVo>(list);
    }

    @Override
    public void upadateAccountLoginDate(Account account) {
        account.setLoginDate(new DateTime());
        accountMapper.updateByPrimaryKeySelective(account);
    }

    @Override
    public int accountRegisterByMerchant(Account account) {
        return accountMapper.insertSelective(account);
    }
    
}
